package network_programming.np1_socket_and_serversocket;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 判断特定的ip地址是否为垃圾邮件发送者的ip地址
 * */
public class SpamCheck {
    public static final String BLACKHOLE = "sbl.spamhaus.org";
    public static boolean isSpammer(String str) {
        try {
            InetAddress address = InetAddress.getByName(str);
            byte[] quad = address.getAddress(); // 获取主机ip地址
            StringBuilder query = new StringBuilder(BLACKHOLE);     // 黑洞列表

            // 反转ip地址的字节，并添加黑洞服务的域
            for(byte octet : quad) {
                int unsignedByte = octet < 0 ? octet + 256 : octet;
                query.insert(0, unsignedByte + ".");
            }
            InetAddress.getByName(query.toString());   // 查找该地址
            return true;
        } catch (UnknownHostException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(SpamCheck.isSpammer("27.56.33.108"));
    }
}
